<?php
namespace app\index\controller;
use app\admin\model\Config;
use app\common\model\From as FromModel;
use think\facade\Request;
use app\admin\traits\Result;
use think\Db;

class Index extends Common
{


    public function index()
    {
        
		$pro_cate = Db::name('cate')->where('parent_id',2)->where('is_nav',1)->order('sort asc,id asc')->select();
		foreach($pro_cate as $k=>$v){
			$cids = Db::name('cate')->where('parent_id',$v['id'])->column('id');
			if($cids){
				array_push($cids,intval($v['id']));
			}else{
				$cids[] = intval($v['id']);
			}
			$pro_cate[$k]['cids'] = $cids;
			//$pro_cate[$k]['pro_list'] = Db::name('article')->where('cate_id','in',$cids)->order('sort asc,a_id asc')->limit(4)->select();
		}
		//dump($pro_cate);die;
		
		$data = Config::where('name', 'index_config')->find();
		
		$case_ids = getAllChildId(1);
        $banner_list = Db::name('banner')->where('is_show',1)->order('sort asc,id desc')->select(); 
        return $this->fetch('',['pro_cate'=>$pro_cate,'index_config'=>$data['value'],'banner_list'=>$banner_list,'case_ids'=>$case_ids]);
    }

	public function tjForm(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			
			$validate = validate('From');
			if (!$validate->check($data)) {
				//令牌数据无效时重置令牌
				$validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
				$msg = Result::error($validate->getError(), null, ['token' => $token]);
				return $msg;
			}
			$FromModel = new FromModel;
            $res = $FromModel->allowField(true)->save($data);
            if ($res) {
                $msg = Result::success('提交成功');
            } else {
                $msg = Result::error('提交失败');
            }
			return $msg;

        } 
        
    }
	
    public function search()
    {
        $kw = $this->request->param('kw');
		$cate_id = Db::name('cate')->where('type','in','1,2')->column('id');
        $article_list = Db::name('article')->where('cate_id','in',$cate_id)->where('title','like',"%$kw%")->paginate(12,false);
        return $this->fetch('',['article_list'=>$article_list]);
    }

}
